package com.zhuqi.util;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.zhuqi.netty.bootstrap.ChatClient;
import com.zhuqi.proto.ChatMessage;
import com.zhuqi.proto.LogOutMessageProto;
import com.zhuqi.proto.LoginMessageProto;
import com.zhuqi.proto.build.ChatMessageBuild;
import com.zhuqi.proto.build.LogOutMessageBuild;
import com.zhuqi.proto.build.LoginMessageProtoBuild;
import com.zhuqi.proto.cost.MessageProtocolBufEnum;
import com.zhuqi.session.messge.MessagePackage;
import com.zhuqi.user.UserCache;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;

import javax.websocket.Session;

/**
 * <p>Netty 操作工具类</p>

 * @author zhuqi
 * @since 2023/4/19 10:00
 */
@Slf4j
public class NettyChatUtil {

	/**
	 * 用户登陆到 Netty 服务
	 */
	public static Boolean doLogging(String userName, String password, Session session) {
		Boolean flag;
		try {
			ChannelHandlerContext ctx = ChatClient.ctx;
			Assert.isTrue(ObjectUtil.isNotEmpty(ctx), "登陆时， ChannelHandlerContext 为空");
			LoginMessageProto.LoginMessageRequest loginRequestMessage = LoginMessageProtoBuild.createLoginRequestMessage(
					userName, password);
			MessagePackage messagePackage = new MessagePackage();
			messagePackage.setBusinessCode(MessageProtocolBufEnum.LOGIN_REQUEST_MESSAGE.getCode());
			messagePackage.setData(loginRequestMessage.toByteArray());
			messagePackage.setSerializeCode((byte) 2);
			messagePackage.setOrderNum(0);
			// 发送消息
			ctx.writeAndFlush(messagePackage);
			log.info("登陆消息发送成功,等待后续操作...");
			flag = Boolean.TRUE;
		} catch (Throwable e) {
			log.info("登陆netty 出现异常：{}", e.getMessage());
			flag = Boolean.FALSE;
			String message = e.getMessage();
			WebsocketUtil.sendMessage(session.getId(), StrUtil.format("登陆出现异常：{}", message));
		}
		return flag;
	}

	/**
	 * 退出 Netty登陆
	 */
	@Deprecated
	public static void exit() {
		ChatClient.exit.set(true);
		ChatClient.ctx.channel().close();
	}

	/**
	 * 发送消息
	 * @param userName 用户名
	 * @param content 内容
	 * @param session session
	 */
	public static void sendMessage(String userName, String content, Session session) {
		ChannelHandlerContext ctx = ChatClient.ctx;
		UserCache userCache = WebsocketUtil.getCurrentUser(session.getId());
		ChatMessage.ChatMessageRequest chatRequestMessage = ChatMessageBuild.createChatRequestMessageProto(
				userCache.getName(), userName, content);
		MessagePackage chatRequestMessagePackage = new MessagePackage();
		chatRequestMessagePackage.setData(chatRequestMessage.toByteArray());
		chatRequestMessagePackage.setBusinessCode(MessageProtocolBufEnum.CHAT_REQUEST_MESSAGE.getCode());
		ctx.writeAndFlush(chatRequestMessagePackage);
	}

	/**
	 * 用户登出
	 * @param userName 用户名
	 * @param content 消息
	 */
	public static void logOut(String userName, String content, Session session) {
		ChannelHandlerContext ctx = ChatClient.ctx;
		LogOutMessageProto.LogOutMessageRequest logOutRequestMessage = LogOutMessageBuild.createLogOutRequestMessage(
				userName, content, 0);
		MessagePackage chatRequestMessagePackage = new MessagePackage();
		chatRequestMessagePackage.setData(logOutRequestMessage.toByteArray());
		chatRequestMessagePackage.setBusinessCode(MessageProtocolBufEnum.LOG_OUT_REQUEST_MESSAGE.getCode());
		ctx.writeAndFlush(chatRequestMessagePackage);
	}
}
